package cn.com.blackboard.DataModule4B2.BBDataUtils;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import blackboard.admin.data.category.CourseCategoryMembership;
import blackboard.admin.persist.category.CourseCategoryMembershipLoader;
import blackboard.admin.persist.category.CourseCategoryMembershipPersister;
import blackboard.data.ValidationException;
import blackboard.data.category.BbCourseCategory;
import blackboard.data.course.Course;
import blackboard.persist.KeyNotFoundException;
import blackboard.persist.PersistenceException;
import blackboard.persist.category.CourseCategoryDbLoader;
import blackboard.persist.category.CourseCategoryDbPersister;
import blackboard.persist.course.CourseDbLoader;
import blackboard.persist.course.CourseDbPersister;
import cm.com.blackboard.DataModule4B2.customizeConfigUtils.DataSourceKeyUtil;
import cm.com.blackboard.DataModule4B2.customizeConfigUtils.ExtentionAttributeUtil;
import cn.com.blackboard.DataModule4B2.model.CourseInfo;
import cn.com.blackboard.DataModule4B2.util.BatchResultModel.Status;
import cn.com.blackboard.DataModule4B2.util.Condition;

public class CourseUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(CourseUtil.class);
	
	private static CourseDbLoader courseLoader = null;
	private static CourseDbPersister coursePersister = null;
	static {
		try {
			courseLoader = CourseDbLoader.Default.getInstance();
			coursePersister = CourseDbPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in CourseUtil init courseLoader/coursePersister"); 
		}
	}
	
	private static CourseCategoryDbLoader courseCategoryDbLoader = null;
	private static CourseCategoryDbPersister courseCategoryDbPersister = null;
	static {
		try {
			courseCategoryDbLoader = CourseCategoryDbLoader.Default.getInstance();
			courseCategoryDbPersister = CourseCategoryDbPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in CourseUtil init CourseCategoryDbLoader/CourseCategoryDbPersister"); 
		}
	}
	
	private static CourseCategoryMembershipLoader courseCategoryMembershipLoader = null;
	private static CourseCategoryMembershipPersister courseCategoryMembershipPersister = null;
	static {
		try {
			courseCategoryMembershipLoader = CourseCategoryMembershipLoader.Default.getInstance();
			courseCategoryMembershipPersister = CourseCategoryMembershipPersister.Default.getInstance();
		} catch (PersistenceException e) {
			logger.info("error in CourseUtil init CourseCategoryMembershipLoader/CourseCategoryMembershipPersister"); 
		}
	}
	
	
	public static List<Course> getCourseListByCondition(Condition condition){
		List<Course> result = new ArrayList<Course>();	
		return result;
	}
	
	public static List<Course> getAllCourseFromBbPlatform(){
		List<Course> result = null;
		try {
			result = courseLoader.loadAllCourses();
		} catch (KeyNotFoundException e) {
			logger.info("error in getAllCourseFromBbPlatform course_id not found"); 
		} catch (PersistenceException e) {
			logger.info("error in getAllCourseFromBbPlatform persistence issue"); 
		}	
		return result;
	}
	
	public static Status importCourse(CourseInfo courseInfo){
		Course course = new Course();
		course.setCourseId(courseInfo.getCourseId());
		course.setTitle(courseInfo.getCourseName());
		
		if(ExtentionAttributeUtil.hasExtention(course, courseInfo)){
			ExtentionAttributeUtil.setExtentionAttribute(course, courseInfo);
		}
		
		DataSourceKeyUtil.setDataSrouceKey(course, courseInfo);
		
		boolean flagCourseImported = false;
		boolean flagCategoryLoaded = false;
		try {
			coursePersister.persist(course);
			flagCourseImported = true;
		} catch (PersistenceException e2) {
			logger.info("error in importCourse course persistenceException");
			return Status.ImportFailed;
		} catch (ValidationException e2) {
			logger.info("error in importCourse course invalid");
			return Status.ImportFailed;
		}
		BbCourseCategory bbCourseCategory = null;
		try {
			String departmentId = DepartmentUtil.appendSilkCategorySuffix(courseInfo.getDepartmentId());
			bbCourseCategory = courseCategoryDbLoader.loadByBatchUid(departmentId);
			flagCategoryLoaded = true;
		} catch (KeyNotFoundException e1) {
			logger.info("error in importCourse can't find coursecategory");
			return Status.LoadFailed;
		} catch (PersistenceException e1) {
			logger.info("error in importCourse coursecategory persistenceException");
			return Status.LoadFailed;
		}
		CourseCategoryMembership ccm = new CourseCategoryMembership();
		ccm.setCategoryBatchUid(bbCourseCategory.getBatchUid());
		ccm.setCourseSiteBatchUid(course.getBatchUid());
		ccm.setCourseId(course.getId());
		ccm.setCategoryId(bbCourseCategory.getId());
		try {
			courseCategoryMembershipPersister.save(ccm);
			return Status.ImportSuccess;
		} catch (PersistenceException e) {
			logger.info("error in importCourse course&coursecategory persistenceException");
			return Status.regisitFailed;
		} catch (ValidationException e) {
			logger.info("error in importCourse with invalid enrollment of course&coursecategory");
			e.printStackTrace();
			return Status.regisitFailed;
		}
	}
}
